package common.utils;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.activities.MainActivity;
import com.enqualcomm.kids.view.dialog.CommitDialogCallBack;
import com.enqualcomm.kids.view.dialog.MyPromptDialog;

/**
 * Created by Sunzhigang on 2016/4/18.
 */
public class PermissionUtils {

    private static final int ADD_TERMINAL = 0;
    private static final int DELETE_TERMINAL = 1;
    private static final int CHAT = 2;
    private static final int STORAGE = 3;
    private static final int READ_CONTACTS = 4;
    private static final int TERMINAL_INFO = 5;
    private static final int LOCATION = 6;

    /**
     * Activity
     * permission 单个权限的申请
     */
    public static boolean CheckPermission(@NonNull Activity activity, String permission, int requestCode) {
        if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
            return false;
        }
        return true;
    }

    /**
     * Activity
     * permissions 多个权限一起申请
     */
    public static boolean CheckPermission(Activity activity, String[] permissions, int requestCode) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity,
                        permissions, requestCode);
                return false;
            }
        }
        return true;
    }

    public static void onRequestPermissionsResult(Context target, int requestCode, int[] grantResults) {
        switch (requestCode) {
            case ADD_TERMINAL:
                if (verifyPermissions(grantResults)) {
                    ((MainActivity) target).addTerminal();
                } else {
                    showRelationDialog(target, R.string.right_camera, R.string.right_function_add_watch);//二维码扫描
                }
                break;
            case TERMINAL_INFO:
                if (verifyPermissions(grantResults)) {
                    ((MainActivity) target).startTerminalInfoActivity();
                } else {
                    showRelationDialog(target, R.string.right_camera, R.string.right_function_terminal_in);//二维码扫描
                }
                break;
            case DELETE_TERMINAL:
                if (verifyPermissions(grantResults)) {
                    ((MainActivity) target).deleteTerminal();
                } else {
                    showRelationDialog(target, R.string.right_contact, R.string.right_function_contact);//刪除手錶
                }
                break;
            case READ_CONTACTS:
                if (verifyPermissions(grantResults)) {
                    ((MainActivity) target).startPhoneBookActivity();
                } else {
                    showRelationDialog(target, R.string.right_contact, R.string.right_function_contact);//刪除手錶
                }
                break;
            case CHAT:
                if (verifyPermissions(grantResults)) {
                    ((MainActivity) target).startChatActivity();
                } else {
                    showRelationDialog(target, R.string.right_record, R.string.right_function_chat);//微话
                }
                break;
            case STORAGE:
                if (verifyPermissions(grantResults)) {
                } else {
                    showRelationDialog(target, R.string.right_storage, R.string.right_function_app);//微话
                }
                break;
            case LOCATION:
                if (verifyPermissions(grantResults)) {
                } else {
                    showRelationDialog(target, R.string.right_location, R.string.right_function_location);//微话
                }
                break;
            default:
                break;
        }
    }


    /**
     * Checks all given permissions have been granted.
     *
     * @param grantResults results
     * @return returns true if all permissions have been granted.
     */
    static boolean verifyPermissions(int... grantResults) {
        if (grantResults.length == 0) {
            return false;
        }
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    static void showRelationDialog(final Context context, int permissionRightID, int functionNameID) {
        String msg = context.getString(R.string.permission_content_1) + context.getString(R.string.app_name) + context.getString(R.string.permission_content_2)
                + context.getString(permissionRightID) + context.getString(R.string.permission_content_3) + context.getString(functionNameID);
        MyPromptDialog dialog = new MyPromptDialog(context, context.getString(R.string.permission_title), msg, new CommitDialogCallBack() {
            @Override
            public void onCommit() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    return;
                }
                context.startActivity(new Intent(Settings.ACTION_APPLICATION_SETTINGS));
                ((Activity) context).finish();
            }

            @Override
            public void onCancel() {

            }
        });
//        dialog.setButtonName(context.getString(R.string.mobile_settings));
        dialog.show();
    }


}
